####### PACKAGES #########
if (!require(estimatr)) {install.packages("estimatr"); library(estimatr)}
if (!require(modelsummary)) {install.packages("modelsummary"); library(modelsummary)}
if (!require(kableExtra)) {install.packages("kableExtra"); library(kableExtra)}
if (!require(sjPlot)) {install.packages("sjPlot"); library(sjPlot)}
if (!require(dplyr)) {install.packages("dplyr"); library(dplyr)}
if (!require(sjlabelled)) {install.packages("sjlabelled"); library(sjlabelled)}
if (!require(ggpubr)) {install.packages("ggpubr"); library(ggpubr)}
if (!require(dotwhisker)) {install.packages("dotwhisker"); library(dotwhisker)}
if (!require(cregg)) {install.packages("cregg"); library(cregg)}
if (!require(lmtest)) {install.packages("lmtest"); library(lmtest)}
if (!require(sandwich)) {install.packages("sandwhich"); library(sandwhich)}



##############Main Analysis #######################
longconjointdata <- read_csv("longconjointdata.csv") #upon publication the dataset will be uploaded in a repository so that the path can be speficief here
longconjoint <- longconjointdata

## Setting theme
library(ggthemr)
dust_colors <- c("#555555", "#db735c", "#EFA86E", "#9A8A76", "#F3C57B", "#7A6752", "#2A91A2")

custom_palette <- define_palette(
  swatch = dust_colors,
  gradient = c(lower = dust_colors[1], upper = dust_colors[7]),
  background = "#ffffff",  # White background
  text = c("#444444", "#444444"),  # Black text color
  line = c("#6e6e6e", "#6e6e6e"),  # Black line color
  gridline = "#F5F5F5"  # Grey gridline color
)
ggthemr(custom_palette, layout="scientific", text_size = 16, set_theme = TRUE, type = "inner")


############################################ ANALYSIS #####################################################
## H1a ##
stmodelh1a <- lm_robust(Crating ~ CONG_GENDER + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh1a, stars = TRUE)

## H1b: 
stmodelh1b <- lm_robust(Crating ~ std_CONG_AGE + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh1b, stars = TRUE)

###H2a:
stmodelh2a <- lm_robust(Crating ~ std_CONG_CLI + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh2a, stars = TRUE)

###H2B: 
stmodelh2b <- lm_robust(Crating ~ std_CONG_IMM + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh2b, stars = TRUE)

#####H2C: 
stmodelh2c <- lm_robust(Crating ~ std_CONG_TAX + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh2c)

##H3a: 
stmodelh3a <-lm_robust(Crating ~ std_CONG_NARC + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(stmodelh3a, stars = TRUE)

##H3b: 
stmodelh3b <- lm_robust(Crating ~ std_CONG_AGREE + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh3b, stars = TRUE)

##H4a
stmodelh4a <- lm_robust(Crating ~ std_CONG_OPEN + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh4a, stars = TRUE)

##H4b
stmodelh4b <- lm_robust(Crating ~ std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                        se_type = "stata",
                        clusters = Respondent_Serial,
                        data = longconjoint)
modelsummary(stmodelh4b, stars = TRUE)

######table main analysis ####
modelsmain <- list("H1a" = stmodelh1a, "H1b" = stmodelh1b, "H2a" = stmodelh2a, "H2b" = stmodelh2b, "H2c" = stmodelh2c, "H3a" = stmodelh3a, "H3b" = stmodelh3b, "H4a" = stmodelh4a, "H4b" =stmodelh4b)
cm <- c('CONG_GENDER'    = 'Gender C',
        'std_CONG_AGE'    = 'Age C',
        'std_CONG_CLI'    = 'Climate C',
        'std_CONG_IMM'    = 'Immigration C',
        'std_CONG_TAX'    = 'Taxation C',
        'std_CONG_NARC'    = 'Narcissism C',
        'std_CONG_AGREE'    = 'Agreeableness C',
        'std_CONG_OPEN'    = 'Openness C',
        'std_CONG_CON'    = 'Conscientiousness C',
        'politicalinterest'    = 'Political Interest',
        'socialtrust'    = 'Social Trust',
        'socialmedia'    = 'Social Media Use')
modelsummary(modelsmain, coef_map = cm, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions H1-H4: Routes to Representations and its effect on feeling represented', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::add_header_above(c(" " = 1, "Descriptive Representation" = 2, "Substantive Representation" = 3, "Psychological Representation" = 4)) %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 


##total model
totalmodel_lmrobust <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                                 se_type = "stata",
                                 clusters = Respondent_Serial,
                                 data = longconjoint)

cm1 <- c('CONG_GENDER'    = 'Gender C',
         'std_CONG_AGE'    = 'Age C',
         'std_CONG_CLI'    = 'Climate C',
         'std_CONG_ISSUE'    = 'Issue C',
         'std_CONG_PERSONALITY'    = 'Personality C',
         'std_CONG_OPEN'    = 'Openness C',
         'std_CONG_CON'    = 'Conscientiousness C',
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(totalmodel_lmrobust,  coef_map = cm1, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'Total Model: Routes to Representations and their effect on feeling represented', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

###################### EXPLORATORY ANALYSIS #######################################################
##Gender
MAlongconjoint <- filter(longconjoint, gender == 1) #N=2090
MA_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = MAlongconjoint)
modelsummary(MA_totalmodel,  stars = c('*' = .01, '**' = .003, '***' = .001))


FElongconjoint <- filter(longconjoint, gender == 2) #N=2019
FE_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = FElongconjoint)
modelsummary(FE_totalmodel, stars = c('*' = .01, '**' = .003, '***' = .001))

dwplotgender <- dwplot(list(totalmodel_lmrobust, MA_totalmodel, FE_totalmodel),
                       ci = 0.95,
                       dodge_size = 0.4,
                       dot_args = list(size = 2),
                       whisker_args = list(size = 1),
                       dist_args = list(alpha = 1),
                       line_args = list(alpha = 0.75, size = 1),
                       vline = geom_vline(
                         xintercept = 0,
                         colour = "grey60",
                         linetype = 2,
                         vars_order = c("std_CONG_CON", "std_CONG_OPEN", "std_CONG_PERSONALITY", "std_CONG_ISSUE", "std_CONG_AGE", "CONG_Gender"),
                         model_order = c("MA_totalmodel", "FE_totalmodel", "totalmodel_lmrobust"))) %>% # plot line at zero _behind_coefs
  relabel_predictors(
    c(
      CONG_GENDER = "Gender C",
      std_CONG_AGE = "Age C",
      std_CONG_ISSUE = "Issue C",
      std_CONG_PERSONALITY = "Personality Trait C",
      std_CONG_OPEN ="Openness C",
      std_CONG_CON = "Conscientiousness C"
    ) ) + # plot line at zero _behind_ coefs
  xlab("Standardized Coefficient Estimate") + ylab("") + ylim(breaks=c("Conscientiousness C", "Openness C", "Personality Trait C", "Issue C", "Age C", "Gender C")) +
  ggtitle("(a) Predicting Feeling Represented by Gender") +
  theme(
    plot.title = element_text(face = "bold"),
    legend.justification = c(1, 0),
    legend.position = c(1, 0),
    legend.title = element_blank()) +
  scale_colour_discrete(name = "Gender",
                        labels = c("Pooled Analysis", "Male", "Female")
  )

dwplotgender

#table gender
modelsgender <- list("Female Respondents" = FE_totalmodel, "Male Respondents" = MA_totalmodel)

modelsummary(modelsgender, coef_map = cm2, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions of subgroup analysis by respondents gender', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 


##age
longconjoint <- longconjoint %>%  mutate(
  age_factor = case_when(
    (between(age, 18, 25)) ~ "GenZ",
    (between(age, 26, 41)) ~ "Millenial",
    (between(age, 42, 57)) ~ "GenX",
    (between(age, 58, 90)) ~ "Boomer+"))
longconjoint %>% count(age_factor)
longconjoint$age_factor <- factor(longconjoint$age_factor)

GenZlongconjoint <- filter(longconjoint, age_factor == "GenZ") #N=2090
genz_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                             se_type = "stata",
                             clusters = Respondent_Serial,
                             data = GenZlongconjoint)
modelsummary(genz_totalmodel, stars = TRUE)

milllongconjoint <- filter(longconjoint, age_factor == "Millenial") #N=2090
mill_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                             se_type = "stata",
                             clusters = Respondent_Serial,
                             data = milllongconjoint)
modelsummary(mill_totalmodel, stars = TRUE)

genxlongconjoint <- filter(longconjoint, age_factor == "GenX") #N=2090
genx_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                             se_type = "stata",
                             clusters = Respondent_Serial,
                             data = genxlongconjoint)
modelsummary(genx_totalmodel, stars = TRUE)

boomlongconjoint <- filter(longconjoint, age_factor == "Boomer+") #N=2090
boom_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                             se_type = "stata",
                             clusters = Respondent_Serial,
                             data = boomlongconjoint)
modelsummary(boom_totalmodel, stars = TRUE)


dwplotage<- dwplot(list(totalmodel_lmrobust, genz_totalmodel, mill_totalmodel, genx_totalmodel, boom_totalmodel),
                   ci = 0.95,
                   dodge_size = 0.4,
                   dot_args = list(size = 2),
                   whisker_args = list(size = 1),
                   dist_args = list(alpha = 1),
                   line_args = list(alpha = 0.75, size = 1),
                   vline = geom_vline(
                     xintercept = 0,
                     colour = "grey60",
                     linetype = 2,
                     vars_order = c("std_CONG_CON", "std_CONG_OPEN", "std_CONG_PERSONALITY", "std_CONG_ISSUE", "std_CONG_AGE", "CONG_Gender"),
                     model_order = c("genz_totalmodel", "mill_totalmodel", "genx_totalmodel", "boom_totalmodel", "totalmodel_lmrobust"))) %>% # plot line at zero _behind_coefs
  relabel_predictors(
    c(CONG_GENDER = "Gender C",
      std_CONG_AGE = "Age C",
      std_CONG_ISSUE = "Issue C",
      std_CONG_PERSONALITY = "Personality Trait C",
      std_CONG_OPEN ="Openness C",
      std_CONG_CON = "Conscientiousness C"
    ) ) + # plot line at zero _behind_ coefs
  xlab("Standardized Coefficient Estimate") + ylab("") + ylim(breaks=c("Conscientiousness C", "Openness C", "Personality Trait C", "Issue C", "Age C", "Gender C")) +
  ggtitle("(b) Predicting Feeling Represented by Age Category") +
  theme(
    plot.title = element_text(face = "bold"),
    legend.justification = c(1, 0),
    legend.position = c(1, 0),
    legend.title = element_blank()) +
  scale_colour_discrete(name = "Age",
                        labels = c("Pooled Analysis", "Gen Z", "Millenial", "Gen X", "Baby Boomer +")
  )

dwplotage

#table age
modelsage <- list("Gen Z Respondents" = genz_totalmodel, "Millenial Respondents" = mill_totalmodel, "Gen X Respondents" = genx_totalmodel, "Baby Boomer Respondents" = boom_totalmodel)

modelsummary(modelsage, coef_map = cm2, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions of subgroup analysis by respondents gender', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

##politicalinterest 
longconjoint <- longconjoint %>%  mutate(
  polinterest = case_when(
    (between(politicalinterest, 1, 3.5)) ~ "lowinterest",
    (between(politicalinterest, 3.5, 6)) ~ "highinterest"))
longconjoint %>% count(polinterest)
longconjoint$polinterest <- factor(longconjoint$polinterest)

lowlongconjoint <- filter(longconjoint, polinterest == "lowinterest") #N=2090
low_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                            se_type = "stata",
                            clusters = Respondent_Serial,
                            data = lowlongconjoint)
modelsummary(low_totalmodel, stars = TRUE)


highlongconjoint <- filter(longconjoint, polinterest == "highinterest") #N=2019
high_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                             se_type = "stata",
                             clusters = Respondent_Serial,
                             data = highlongconjoint)
modelsummary(high_totalmodel, stars = TRUE)

dwplotpolinterest<- dwplot(list(totalmodel_lmrobust, low_totalmodel, high_totalmodel),
                           ci = 0.95,
                           dodge_size = 0.4,
                           dot_args = list(size = 2),
                           whisker_args = list(size = 1),
                           dist_args = list(alpha = 1),
                           line_args = list(alpha = 0.75, size = 1),
                           vline = geom_vline(
                             xintercept = 0,
                             colour = "grey60",
                             linetype = 2,
                             vars_order = c("std_CONG_CON", "std_CONG_OPEN", "std_CONG_PERSONALITY", "std_CONG_ISSUE", "std_CONG_AGE", "CONG_Gender"),
                             model_order = c("low_totalmodel", "high_totalmodel", "totalmodel_lmrobust")))%>% # plot line at zero _behind_coefs
  relabel_predictors(
    c(
      CONG_GENDER = "Gender C",
      std_CONG_AGE = "Age C",
      std_CONG_ISSUE = "Issue C",
      std_CONG_PERSONALITY = "Personality Trait C",
      std_CONG_OPEN ="Openness C",
      std_CONG_CON = "Conscientiousness C"
    ) ) + # plot line at zero _behind_ coefs
  xlab("Standardized Coefficient Estimate") + ylab("") + ylim(breaks=c("Conscientiousness C", "Openness C", "Personality Trait C", "Issue C", "Age C", "Gender C")) +
  ggtitle("(c) Predicting Feeling Represented by Political Interest") +
  theme( plot.title = element_text(face = "bold"),
         legend.justification = c(1, 0),
         legend.position = c(1, 0),
         legend.title = element_blank()) +
  scale_colour_discrete(name = "Political Interest",
                        labels = c("Pooled Analysis", "Low Political Interest", "High Political Interest")
  )

dwplotpolinterest

#table interest
modelsinterest <- list("Low Political Interest" = low_totalmodel, "High Political Interest" = high_totalmodel)

modelsummary(modelsinterest, coef_map = cm2, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions of subgroup analysis by respondents level of political interest', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 



## COUNTRY ##
#"1" = "DE", "2" = "IT", "3" = "RO", "4" = "SW"
##DE
DElongconjoint <- filter(longconjoint, country == "DE")
DE_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = DElongconjoint)

modelsummary(DE_totalmodel, stars = TRUE)

##IT
ITlongconjoint <- filter(longconjoint, country == "IT") #N=2097
IT_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = ITlongconjoint)
modelsummary(IT_totalmodel, stars = TRUE)

##RO
ROlongconjoint <- filter(longconjoint, country == "RO") #N=2019
RO_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = ROlongconjoint)
modelsummary(RO_totalmodel, stars = TRUE)

##SWEDEN
SWlongconjoint <- filter(longconjoint, country == "SW") #N=2090
SW_totalmodel <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                           se_type = "stata",
                           clusters = Respondent_Serial,
                           data = SWlongconjoint)
modelsummary(SW_totalmodel, stars = TRUE)

##plotting regressions per country

dwplotcountry <- dwplot(list(totalmodel_lmrobust, SW_totalmodel, DE_totalmodel, IT_totalmodel, RO_totalmodel),
                        ci = 0.95,
                        dodge_size = 0.4,
                        dot_args = list(size = 2),
                        whisker_args = list(size = 1),
                        dist_args = list(alpha = 1),
                        line_args = list(alpha = 0.75, size = 1),
                        vline = geom_vline(
                          xintercept = 0,
                          colour = "grey60",
                          linetype = 5,
                          vars_order = c("std_CONG_CON", "std_CONG_OPEN", "std_CONG_PERSONALITY", "std_CONG_ISSUE", "std_CONG_AGE", "CONG_Gender"),
                          model_order = c("RO_totalmodel", "IT_totalmodel", "DE_totalmodel", "SW_totalmodel", "totalmodel_lmrobust"))) %>% # plot line at zero _behind_coefs
  relabel_predictors(
    c(
      CONG_GENDER = "Gender C",
      std_CONG_AGE = "Age C",
      std_CONG_ISSUE = "Issue C",
      std_CONG_PERSONALITY = "Personality Trait C",
      std_CONG_OPEN = "Openness C",
      std_CONG_CON = "Conscientiousness C"
    ) ) + # plot line at zero _behind_ coefs
  xlab("Standardized Coefficient Estimate") + ylab("") + ylim(breaks=c("Conscientiousness C", "Openness C", "Personality Trait C", "Issue C", "Age C", "Gender C")) +
  ggtitle("(d) Predicting Feeling Represented by Country") +
  theme(
    plot.title = element_text(face = "bold"),
    legend.justification = c(1, 0),
    legend.position = c(1, 0),
    legend.title = element_blank()
  ) +
  scale_colour_discrete(name = "Country",
                        labels = c("Pooled Analysis", "Sweden", "Germany", "Italy", "Romania")
  )

dwplotcountry 


##Table for country models
modelscountry <- list("Sweden" = SW_totalmodel, "Germany" = DE_totalmodel, "Italy" = IT_totalmodel, "Romania" = RO_totalmodel)
cm2 <- c("CONG_GENDER" = "Gender C",
         "std_CONG_AGE" = "Age C",
         "std_CONG_ISSUE" = "Issue C",
         "std_CONG_PERSONALITY" = "Personality Trait C",
         "std_CONG_OPEN" ="Openness C",
         "std_CONG_CON" = "Conscientiousness C",
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(modelscountry, coef_map = cm2, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions of subgroup analysis by country of analysis', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 


##Only add the following to save the plots
ggsave("dwplotgender600.png", plot = dwplotgender, width = 10, height = 8, dpi = 600)
ggsave("dwplotage600.png", plot = dwplotage, width = 10, height = 8, dpi = 600)
ggsave("dwplotinterest600.png", plot = dwplotpolinterest, width = 10, height = 8, dpi = 600)
ggsave("dwplotcountry600.png", plot = dwplotcountry, width = 10, height = 8, dpi = 600)




#######################################################
####################################################### APPENDIX ANALYSIS #########################################################
#######################################################


####MAIN EFFECTS ANALYSIS: CONJOINT: Appendix C1
##making original varibles factors
longconjoint$gender_conjoint <- factor(longconjoint$Cgender, levels = rev(unique(longconjoint$Cgender)))
longconjoint$age_conjoint <- factor(longconjoint$Cage, levels = rev(unique(longconjoint$Cage)))
longconjoint$issueposition_conjoint <- factor(longconjoint$Cissueposition, levels = rev(unique(longconjoint$Cissueposition)))
longconjoint$personality_conjoint <- factor(longconjoint$Cpersonality, levels = rev(unique(longconjoint$Cpersonality)))

longconjoint$gender_conjoint <- recode_factor(longconjoint$gender_conjoint, "1" = "male", 
                                              "2" = "female")
longconjoint$age_conjoint <- recode_factor(longconjoint$age_conjoint, "1" = "GenZ", "2" = "Millenial", "3" = "GenX", "4" = "Boomer")
longconjoint$issueposition_conjoint  <- recode_factor(longconjoint$issueposition_conjoint, "1" = "proclimate", "2" = "anticlimate", "3" = "proimmigration", "4" = "antiimmigration", "5" = "protax", "6" = "antitax")
longconjoint$personality_conjoint <- recode_factor(longconjoint$personality_conjoint, "1" = "highnarcissism", "2" = "lownarcissism", "3" = "highagreeableness", "4" = "lowagreeableness")

###Main effects MM 
mm1 <- mm(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial)
plot(mm1, vline = 0.5)
head(mm1[c("feature", "level", "estimate", "std.error")], 20L)

#### Appendic C.2: A total model ########
##total model
totalmodel_lmrobust <- lm_robust(Crating ~ CONG_GENDER + std_CONG_AGE + std_CONG_ISSUE + std_CONG_PERSONALITY + std_CONG_OPEN + std_CONG_CON + politicalinterest + socialtrust + socialmedia,
                                 se_type = "stata",
                                 clusters = Respondent_Serial,
                                 data = longconjoint)

cm1 <- c('CONG_GENDER'    = 'Gender C',
         'std_CONG_AGE'    = 'Age C',
         'std_CONG_CLI'    = 'Climate C',
         'std_CONG_ISSUE'    = 'Issue C',
         'std_CONG_PERSONALITY'    = 'Personality C',
         'std_CONG_OPEN'    = 'Openness C',
         'std_CONG_CON'    = 'Conscientiousness C',
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(totalmodel_lmrobust,  coef_map = cm1, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'Total Model: Routes to Representations and their effect on feeling represented', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent'),
             output = 'latex') %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

####################### ROBUSTNESS CHECKS: ALTERNATIVE OLS MODELS (APPENDIX C3)#################################
#### Appendix C.3.1: as preregistered####
## H1a ##
modelh1a <- lm_robust(Crating ~ CONG_GENDER + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh1a, stars = TRUE)

## H1b: 
modelh1b <- lm_robust(Crating ~ CONG_AGE + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh1b, stars = TRUE)

###H2a:
modelh2a <- lm_robust(Crating ~ CONG_CLI + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh2a, stars = TRUE)

###H2B: 
modelh2b <- lm_robust(Crating ~ CONG_IMM + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh2b, stars = TRUE)

#####H2C: 
modelh2c <- lm_robust(Crating ~ CONG_TAX + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh2c, stars = TRUE)

##H3a: 
modelh3a <-lm_robust(Crating ~ CONG_NARC + politicalinterest + socialtrust + socialmedia,
                     se_type = "stata",
                     clusters = Respondent_Serial,
                     data = longconjoint)
modelsummary(modelh3a, stars = TRUE)

##H3b: 
modelh3b <- lm_robust(Crating ~ CONG_AGREE + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh3b, stars = TRUE)

##H4a
modelh4a <- lm_robust(Crating ~ CONG_OPEN + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh4a, stars = TRUE)

##H4b
modelh4b <- lm_robust(Crating ~ CONG_CON + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(modelh4b, stars = TRUE)

modelsprereg <- list("H1a" = modelh1a, "H1b" = modelh1b, "H2a" = modelh2a, "H2b" = modelh2b, "H2c" = modelh2c, "H3a" = modelh3a, "H3b" = modelh3b, "H4a" = modelh4a, "H4b" =modelh4b)
cm2 <- c('CONG_GENDER'    = 'Gender C',
         'CONG_AGE'    = 'Age C',
         'CONG_CLI'    = 'Climate C',
         'CONG_IMM'    = 'Immigration C',
         'CONG_TAX'    = 'Taxation C',
         'CONG_NARC'    = 'Narcissism C',
         'CONG_AGREE'    = 'Agreeableness C',
         'CONG_OPEN'    = 'Openness C',
         'CONG_CON'    = 'Conscientiousness C',
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(modelsprereg, coef_map = cm2, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions H1-H4: Routes to Representations and its effect on feeling represented', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::add_header_above(c(" " = 1, "Descriptive Representation" = 2, "Substantive Representation" = 3, "Psychological Representation" = 4)) %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 


############## Appendic C.3.2: Alternatvie 1: Binary Congruences#########
## H1a ##
Bmodelh1a <- lm_robust(Crating ~ CONG_GENDER + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh1a, stars = TRUE)

## H1b: 
Bmodelh1b <- lm_robust(Crating ~ CONG_AGE_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh1b, stars = TRUE)

###H2a:
Bmodelh2a <- lm_robust(Crating ~ CONG_CLI_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh2a, stars = TRUE)

###H2B: 
Bmodelh2b <- lm_robust(Crating ~ CONG_IMM_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh2b, stars = TRUE)

#####H2C: 
Bmodelh2c <- lm_robust(Crating ~ CONG_TAX_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh2c, stars = TRUE)

##H3a: 
Bmodelh3a <-lm_robust(Crating ~ CONG_NARC_B + politicalinterest + socialtrust + socialmedia,
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(Bmodelh3a, stars = TRUE)

##H3b: 
Bmodelh3b <- lm_robust(Crating ~ CONG_AGREE_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh3b, stars = TRUE)

##H4a
Bmodelh4a <- lm_robust(Crating ~ CONG_OPEN_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh4a, stars = TRUE)

##H4b
Bmodelh4b <- lm_robust(Crating ~ CONG_CON_B + politicalinterest + socialtrust + socialmedia,
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Bmodelh4b, stars = TRUE)

modelsbinary <- list("H1a" = Bmodelh1a, "H1b" = Bmodelh1b, "H2a" = Bmodelh2a, "H2b" = Bmodelh2b, "H2c" = Bmodelh2c, "H3a" = Bmodelh3a, "H3b" = Bmodelh3b, "H4a" = Bmodelh4a, "H4b" = Bmodelh4b)
cm3 <- c('CONG_GENDER'    = 'Gender C',
         'CONG_AGE_B'    = 'Age C',
         'CONG_CLI_B'    = 'Climate C',
         'CONG_IMM_B'    = 'Immigration C',
         'CONG_TAX_B'    = 'Taxation C',
         'CONG_NARC_B'    = 'Narcissism C',
         'CONG_AGREE_B'    = 'Agreeableness C',
         'CONG_OPEN_B'    = 'Openness C',
         'CONG_CON_B'    = 'Conscientiousness C',
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(modelsbinary, coef_map = cm3, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions H1-H4: Routes to Representations and its effect on feeling represented', 
             notes = list('Congruences are coded binary (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::add_header_above(c(" " = 1, "Descriptive Representation" = 2, "Substantive Representation" = 3, "Psychological Representation" = 4)) %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

######Appendix C.3.3 . Robustness check Alternative 2: Probit Regression#######
##h1a
probith1a <- glm(chosen ~ CONG_GENDER + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith1a_clustered <- coeftest(probith1a,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

##H1b
probith1b <- glm(chosen ~ std_CONG_AGE + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith1b_clustered <- coeftest(probith1b,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

###H2a:
probith2a <- glm(chosen ~ std_CONG_CLI + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith2a_clustered <- coeftest(probith2a,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

##H2b
probith2b <- glm(chosen ~ std_CONG_IMM + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith2b_clustered <- coeftest(probith2b,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

##H2c
probith2c <- glm(chosen ~ std_CONG_TAX + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith2c_clustered <- coeftest(probith2c,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

##H3a
probith3a <- glm(chosen ~ std_CONG_NARC + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith3a_clustered <- coeftest(probith3a,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)
##H3b
probith3b <- glm(chosen ~ std_CONG_AGREE + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith3b_clustered <- coeftest(probith3b,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)
##H4a
probith4a <- glm(chosen ~ std_CONG_OPEN + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith4a_clustered <- coeftest(probith4a,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

##H4b
probith4b <- glm(chosen ~ std_CONG_CON + politicalinterest + socialtrust + socialmedia, 
                 family = binomial(link = "probit"), #default is logit 
                 data = longconjoint)
probith4b_clustered <- coeftest(probith4b,
                                vcov = vcovCL,
                                type = "HC1",
                                cluster = ~Respondent_Serial)

modelsprobit <- list("H1a" = probith1a_clustered, "H1b" = probith1b_clustered, "H2a" = probith2a_clustered, "H2b" = probith2b_clustered, "H2c" = probith2c_clustered, "H3a" = probith3a_clustered, "H3b" = probith3b_clustered, "H4a" = probith4a_clustered, "H4b" =probith4b_clustered)
cm4 <- c('CONG_GENDER'    = 'Gender C',
         'std_CONG_AGE'    = 'Age C',
         'std_CONG_CLI'    = 'Climate C',
         'std_CONG_IMM'    = 'Immigration C',
         'std_CONG_TAX'    = 'Taxation C',
         'std_CONG_NARC'    = 'Narcissism C',
         'std_CONG_AGREE'    = 'Agreeableness C',
         'std_CONG_OPEN'    = 'Openness C',
         'std_CONG_CON'    = 'Conscientiousness C',
         'politicalinterest'    = 'Political Interest',
         'socialtrust'    = 'Social Trust',
         'socialmedia'    = 'Social Media Use')
modelsummary(modelsprobit, coef_map = cm4, gof_omit = 'DF|Deviance|Std.Errors|BIC|AIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions H1-H4: Probit Regression', 
             notes = list('Congruences are rescaled (0-1)', 'Probit Regression, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent)'),
             output = 'latex') %>%
  kableExtra::add_header_above(c(" " = 1, "Descriptive Representation" = 2, "Substantive Representation" = 3, "Psychological Representation" = 4)) %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

######Appendix C.3.4 . Robustness check country controls#######
## H1a ##
Fmodelh1a <- lm_robust(Crating ~ CONG_GENDER + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh1a, stars = TRUE)

## H1b: 
Fmodelh1b <- lm_robust(Crating ~ std_CONG_AGE + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh1b, stars = TRUE)

###H2a:
Fmodelh2a <- lm_robust(Crating ~ std_CONG_CLI + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh2a, stars = TRUE)

###H2B: 
Fmodelh2b <- lm_robust(Crating ~ std_CONG_IMM + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh2b, stars = TRUE)

#####H2C: 
Fmodelh2c <- lm_robust(Crating ~ std_CONG_TAX + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh2c)

##H3a: 
Fmodelh3a <-lm_robust(Crating ~ std_CONG_NARC + politicalinterest + socialtrust + socialmedia + factor(country),
                      se_type = "stata",
                      clusters = Respondent_Serial,
                      data = longconjoint)
modelsummary(Fmodelh3a, stars = TRUE)

##H3b: 
Fmodelh3b <- lm_robust(Crating ~ std_CONG_AGREE + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh3b, stars = TRUE)

##H4a
Fmodelh4a <- lm_robust(Crating ~ std_CONG_OPEN + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh4a, stars = TRUE)

##H4b
Fmodelh4b <- lm_robust(Crating ~ std_CONG_CON + politicalinterest + socialtrust + socialmedia + factor(country),
                       se_type = "stata",
                       clusters = Respondent_Serial,
                       data = longconjoint)
modelsummary(Fmodelh4b, stars = TRUE)

######table country controls ####
modelsfixed <- list("H1a" = Fmodelh1a, "H1b" = Fmodelh1b, "H2a" = Fmodelh2a, "H2b" = Fmodelh2b, "H2c" = Fmodelh2c, "H3a" = Fmodelh3a, "H3b" = Fmodelh3b, "H4a" = Fmodelh4a, "H4b" =Fmodelh4b)
cmcountry <- c('CONG_GENDER'    = 'Gender C',
               'std_CONG_AGE'    = 'Age C',
               'std_CONG_CLI'    = 'Climate C',
               'std_CONG_IMM'    = 'Immigration C',
               'std_CONG_TAX'    = 'Taxation C',
               'std_CONG_NARC'    = 'Narcissism C',
               'std_CONG_AGREE'    = 'Agreeableness C',
               'std_CONG_OPEN'    = 'Openness C',
               'std_CONG_CON'    = 'Conscientiousness C',
               'politicalinterest'    = 'Political Interest',
               'socialtrust'    = 'Social Trust',
               'socialmedia'    = 'Social Media Use',
               'factor(country)IT' = '(country)IT',
               'factor(country)RO' =  '(country)RO',
               'factor(country)SW' = '(country)SW')
modelsummary(modelsfixed, coef_map = cmcountry, gof_omit = 'DF|Deviance|Std.Errors|BIC',
             stars = c('*' = .01, '**' = .003, '***' = .001),
             coef_omit = "Intercept", title = 'OLS Regressions H1-H4: Controlling for Country Differences.', 
             notes = list('Congruences are rescaled (0-1)', 'OLS regression estimates, robust standard errors in parantheses. Standard errors are clustered at the level of the respondent.', 'Country controls included with Germany as the reference category to account for cross-country differences.'),
             output = 'latex') %>%
  kableExtra::add_header_above(c(" " = 1, "Descriptive Representation" = 2, "Substantive Representation" = 3, "Psychological Representation" = 4)) %>%
  kableExtra::kable_styling(font_size = 7, full_width = F, bootstrap_options = c("condensed")) 

###Marginal Means: subgroup analysis######
##making factor variables (numeric does not work with ACME and MM)
longconjoint$gender_congruence <- factor(longconjoint$CONG_GENDER)
longconjoint$age_congreunce <- factor(longconjoint$CONG_AGE_B)
longconjoint$issue_congruence <- factor(longconjoint$CONG_ISSUE_B)
longconjoint$personality_congruence <- factor(longconjoint$CONG_PERSONALITY_B)
longconjoint$openness_congruence <- factor(longconjoint$CONG_OPEN_B)
longconjoint$conscientiousness_congruence <- factor(longconjoint$CONG_CON_B)

longconjoint$gender_congruence <- recode_factor(longconjoint$gender_congruence, "0" = "incongruentgender", "1" = "congruentgender")
longconjoint$age_congreunce <- recode_factor(longconjoint$age_congreunce, "0" = "incongruentage",  "1" = "congruentage")
longconjoint$issue_congruence <- recode_factor(longconjoint$issue_congruence, "0" = "incongruentissue", "1" = "congruentissue", )
longconjoint$personality_congruence <- recode_factor(longconjoint$personality_congruence, "0" = "incongruentpersonality", "1" = "congruentpersonality", )
longconjoint$openness_congruence <- recode_factor(longconjoint$openness_congruence, "0" = "incongruentopenness", "1" = "congruentopenness")
longconjoint$conscientiousness_congruence <- recode_factor(longconjoint$conscientiousness_congruence, "0" = "incongruentcon", "1" = "congruentcon")

######Appendix C.3.5 - Robustness check MM subgroups ########
#gender
longconjoint$Fgender <- factor(longconjoint$gender)
longconjoint$Fgender <- recode_factor(longconjoint$Fgender, "1" = "male", 
                                      "2" = "female", "3" = "other")
mm1_gender <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~Fgender)
mmplot_gender <- plot(mm1_gender, vline = 0.5, group = "Fgender") +
  labs(title = "H1a") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_gender, 40L)

#subgroup age
longconjoint <- longconjoint %>%  mutate(
  age_B = case_when(
    (between(age, 18, 25)) ~ "GenZ",
    (between(age, 26, 41)) ~ "Millenial",
    (between(age, 42, 57)) ~ "GenX",
    (between(age, 58, 90)) ~ "Boomer+"))
longconjoint %>% count(age_B)
longconjoint$age_B <- factor(longconjoint$age_B)
mm1_age <- cj(longconjoint, chosen ~ chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~age_B)
mmplot_age <-plot(mm1_age, vline = 0.5, group = "age_B") +
  labs(title = "H1b") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )

#subgroupclimate
longconjoint <- longconjoint %>%  mutate(
  climateposition_B = case_when(
    (between(climateposition, 1, 5)) ~ "ProClimate",
    (between(climateposition, 5, 10)) ~ "AntiClimate"))
longconjoint %>% count(climateposition_B)
longconjoint$climateposition_B <- factor(longconjoint$climateposition_B)
mm1_climate <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~climateposition_B)
mmplot_climate <- plot(mm1_climate, vline = 0.5, group = "climateposition_B") +
  labs(title = "H2a") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )

head(mm1_climate, 40L)

#subgroupimmigration
longconjoint <- longconjoint %>%  mutate(
  immigrationposition_B = case_when(
    (between(immigrationposition, 1, 5)) ~ "ProImmigration",
    (between(immigrationposition, 5, 10)) ~ "AntiImmigration"))
longconjoint %>% count(immigrationposition_B)
longconjoint$immigrationposition_B <- factor(longconjoint$immigrationposition_B)
mm1_immigration <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~immigrationposition_B)
mmplot_immigration <-plot(mm1_immigration, vline = 0.5, group = "immigrationposition_B") +
  labs(title = "H2b") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_immigration, 40L)

#subgrouptax
longconjoint <- longconjoint %>%  mutate(
  taxposition_B = case_when(
    (between(taxposition, 1, 5)) ~ "ProTax",
    (between(taxposition, 5, 10)) ~ "AntiTax"))
longconjoint %>% count(taxposition_B)
longconjoint$taxposition_B <- factor(longconjoint$taxposition_B)
mm1_tax <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~taxposition_B)
mmplot_tax <-plot(mm1_tax, vline = 0.5, group = "taxposition_B") +
  labs(title = "H2c") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_tax, 40L)
#subgroupnarcissism
longconjoint <- longconjoint %>%  mutate(
  narcs_B = case_when(
    (between(narcs, 1, 3)) ~ "lownarcs",
    (between(narcs, 3, 5)) ~ "highnarcs"))
longconjoint %>% count(narcs_B)
longconjoint$narcs_B <- factor(longconjoint$narcs_B)
mm1_narcs <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~narcs_B)
mmplot_narcs <-plot(mm1_narcs, vline = 0.5, group = "narcs_B") +
  labs(title = "H3a") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_narcs, 40L)
#subgroupagreeableness
longconjoint <- longconjoint %>%  mutate(
  agree_B = case_when(
    (between(agree, 1, 3)) ~ "lowagree",
    (between(agree, 3, 5)) ~ "highagree"))
longconjoint %>% count(agree_B)
longconjoint$agree_B <- factor(longconjoint$agree_B)
mm1_agree <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~agree_B)
mmplot_agree <-plot(mm1_agree, vline = 0.5, group = "agree_B") +
  labs(title = "H3b") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_agree, 40L)
#subgroupopen
longconjoint <- longconjoint %>%  mutate(
  open_B = case_when(
    (between(open, 1, 3)) ~ "lowopen",
    (between(open, 3, 5)) ~ "highopen"))
longconjoint %>% count(open_B)
longconjoint$open_B <- factor(longconjoint$open_B)
mm1_open <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~open_B)
mmplot_open <-plot(mm1_open, vline = 0.5, group = "open_B")+
  labs(title = "H4a") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_open, 40L)
#subgroupcon
longconjoint <- longconjoint %>%  mutate(
  con_B = case_when(
    (between(consc, 1, 3)) ~ "lowcon",
    (between(consc, 3, 5)) ~ "highcon"))
longconjoint %>% count(con_B)
longconjoint$con_B <- factor(longconjoint$con_B)
mm1_con <- cj(longconjoint, chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint, id = ~Respondent_Serial, estimate = "mm", by = ~con_B)
mmplot_con <-plot(mm1_con, vline = 0.5, group = "con_B") +
  labs(title = "H4b") +
  theme( plot.title = element_text(size = rel(2)),
         legend.title= element_blank(),
         legend.position = c(.95, .95),
         legend.justification = c("right", "top"),
         legend.box.just = "right",
         legend.margin = margin(6, 6, 6, 6)
  )
head(mm1_con, 40L)
ggarrange(mmplot_gender, mmplot_age, mmplot_immigration, mmplot_climate, mmplot_tax, mmplot_narcs, mmplot_agree, mmplot_open, mmplot_con, ncol = 2, nrow = 5) 


###### ACME and MM: APPENDIX C3.6 ########
###Congruences MM and ACME: routes to representation
mm2 <- mm(longconjoint, chosen ~ gender_congruence + age_congreunce + issue_congruence + personality_congruence + openness_congruence + conscientiousness_congruence, id = ~Respondent_Serial)
mm2plot <- plot(mm2, vline = 0.5)
#ACME
amces2 <- cj(longconjoint, chosen ~ gender_congruence + age_congreunce + issue_congruence + personality_congruence + openness_congruence + conscientiousness_congruence, id = ~Respondent_Serial)
acme2plot <- plot(amces2)
ggarrange(mm2plot, acme2plot, common.legend = TRUE)
head(mm2 [c("feature", "level", "estimate", "std.error")], 20L)
head(amces2 [c("feature", "level", "estimate", "std.error")], 20L)



